$(function() {
    homepage.init(); //原始
    homepage.start_select_click(); //选择出发城市
    homepage.reach_select_click(); //选择到达城市
    homepage.start_select_blur(); //选择出发城市失去焦点
    homepage.reach_select_blur(); //选择到达城市失去焦点
    homepage.city_mouseover(); //下列菜单鼠标触碰
    homepage.city_mouseout(); //下列菜单鼠标来开
    homepage.delete_click(); //删除航班显示
    homepage.btn_click(); //查询
});

var city_key = 0;
var rooturl = "http://uneven.live:8080";
var s_cityURL = rooturl + "/search/destination";
var r_cityURL = rooturl + "/search/source";
var flight_listURL = rooturl + "/search";
var homepage = {
    init: function() {
        var myDate = new Date();
        var year = myDate.getFullYear() + 500,
            month = myDate.getMonth() + 1,
            day = myDate.getDate();
        var winHeight = window.innerHeight;
        if (month < 10) {
            month = '0' + month;
        }
        $('#airplane_date').val(year + "-" + month + "-" + day);
        $('#tbody').css('height', winHeight);
        //载入出发城市

        $.ajax({
            type: "get",
            url: s_cityURL,
            dataType: "json",
            contentType: "application/json",
            success: function(data) {
                if (data.Code == 0) {
                    for (var i = 0; i < data.Data.length; i++) {
                        $('#reach_city').find('ul').append('<li></li>');
                        $('#reach_city').find('ul').children(':last').text(data.Data[i]);
                    }
                } else {
                    layer.alert("连接失败！");
                }
            },
            fail: function() {
                layer.alert("无法连接服务器！");
            }
        });

        //载入到达城市
        $.ajax({
            type: "get",
            url: r_cityURL,
            dataType: "json",
            contentType: "application/json",
            success: function(data) {
                if (data.Code == 0) {
                    for (var i = 0; i < data.Data.length; i++) {
                        $('#start_city').find('ul').append('<li></li>');
                        $('#start_city').find('ul').children(':last').text(data.Data[i]);
                    }
                } else {
                    layer.alert("连接失败！");
                }
            },
            fail: function() {
                layer.alert("无法连接服务器！");
            }
        });

    },

    start_select_click: function() {
        $('#start_select').click(function() {
            $('#start_city').removeClass('hidden');
            $('#reach_d').css('display', 'none');
            $('#date_d').css('display', 'none');
            $('#btn_d').css('display', 'none');

        });
    },

    start_select_blur: function() {
        $('#start_select').blur(function() {
            if (city_key == 0) {
                method.start_select_blur_result();
            }
        });

    },

    reach_select_click: function() {
        $('#reach_select').click(function() {
            $('#reach_city').removeClass('hidden');
            $('#date_d').css('display', 'none');
            $('#btn_d').css('display', 'none');
        });
    },

    reach_select_blur: function() {
        $('#reach_select').blur(function() {
            if (city_key == 0) {
                method.reach_select_blur_result();
            }
        });
    },

    city_mouseover: function() {
        $('#start_city').mouseover(function() {
            city_key = 1;
            $('#start_city').find('li').mouseover(function() {
                $(this).addClass('city_change');
            });
            $('#start_city').find('li').mouseout(function() {
                $(this).removeClass('city_change');
            });
            $('#start_city').find('li').click(function() {
                var li_text = $(this).text();
                $('#start_select').find('option').text(li_text);
                method.start_select_blur_result();
            });
        });

        $('#reach_city').mouseover(function() {
            city_key = 1;
            $('#reach_city').find('li').mouseover(function() {
                $(this).addClass('city_change');
            });
            $('#reach_city').find('li').mouseout(function() {
                $(this).removeClass('city_change');
            });
            $('#reach_city').find('li').click(function() {
                var li_text = $(this).text();
                $('#reach_select').find('option').text(li_text);
                method.reach_select_blur_result();
            });
        });
    },

    city_mouseout: function() {
        $('#start_city').mouseout(function() {
            city_key = 0;
        });

        $('#reach_city').mouseout(function() {
            city_key = 0;
        });
    },

    delete_click: function() {
        $('#delete').click(function() {
            window.location.reload();
        });
    },

    btn_click: function() {
        $('#btn').click(function() {
            var s_city = $('#start_select').find('option').text();
            var r_city = $('#reach_select').find('option').text();
            var time = $('#airplane_date').val();
            var details = {
                source: s_city,
                destination: r_city,
                datum: time,
            };
            if (s_city != "选择出发地点") {
                if (r_city != "选择到达地点") {
                    $('#flight_d').removeClass('hidden');
                    $('#f_start').text(s_city);
                    $('#f_reach').text(r_city);
                    $('#f_time').text(time);
                    $.ajax({
                        type: "POST",
                        url: flight_listURL,
                        data: details,
                        success: function(data) {
                            if (data.Code == 0) {
                                $('#table_tbody').find('tr').remove();
                                if (data.Data.length != 0) {
                                    for (var i = 0; i < data.Data.length; i++) {
                                        $('#table_thead').removeClass('hidden');
                                        $('#table_tbody').append('<tr><td></td><td></td><td></td><td></td><td></td></tr>');
                                        $('#table_tbody').children(':last').children(':eq(0)').text(data.Data[i].Flightnum);
                                        $('#table_tbody').children(':last').children(':eq(1)').text(data.Data[i].Aircraftmodel);
                                        $('#table_tbody').children(':last').children(':eq(2)').text(data.Data[i].Begintime);
                                        $('#table_tbody').children(':last').children(':eq(3)').text(data.Data[i].Endtime);
                                        $('#table_tbody').children(':last').children(':eq(4)').text(data.Data[i].Price);
                                    }
                                } else {
                                    $('#table_thead').addClass('hidden');
                                    $('#table_tbody').append('<tr>' +
                                        '<td colspan="5" class="no_flight">' +
                                        '所选日期无航班！' +
                                        '</td></tr>');
                                }
                            } else {
                                layer.alert("连接失败！");
                            }
                        },
                        fail: function() {
                            layer.alert("无法连接服务器！");
                        }
                    });
                } else {
                    layer.alert("请选择到达地点");
                }
            } else {
                layer.alert("请选择出地点");
            }
        });
    }
};

var method = {
    start_select_blur_result: function() {
        $('#start_city').addClass('hidden');
        $('#reach_d').css('display', 'block');
        $('#date_d').css('display', 'block');
        $('#btn_d').css('display', 'block');
    },

    reach_select_blur_result: function() {
        $('#reach_city').addClass('hidden');
        $('#date_d').css('display', 'block');
        $('#btn_d').css('display', 'block');
    },
}